public class TreeWay {

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    class Solution {
        public boolean isBalanced(TreeNode root) {
            if (root == null) {
                return true;
            }
            int lefttree1 = getHigh(root.left);
            int righttree1 = getHigh(root.right);
            return Math.abs(lefttree1 - righttree1) <= 1 && isBalanced(root.left) && isBalanced(root.right);


        }

        public int getHigh(TreeNode root) {
            if (root == null) {
                return 0;
            }
            int lefttree = getHigh(root.left);
            int righttree = getHigh(root.right);

            return lefttree > righttree ? lefttree + 1 : righttree + 1;
        }
    }
}
